﻿using SharPy.GameObjects;
using SharPy.WebObjects;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;
using Windows.Data.Json;
using Windows.Storage;

namespace SharPy.GameSaves
{
    class clGameSave
    {
        private int lineNo;

        clGameSave() { this.lineNo = 0; }

        public static async Task<int> WriteSave(clGame pGame, int pSlot)
        {
            StorageFile f = await File_Management.clFileHelper.GetSaveFile(pGame.RootFolder);
            
            //TODO: Completely overwriting data, check if partial replace is possible
            SaveDataItem newSave = new SaveDataItem(pSlot, SharPy.Parser.clText.getLine(pGame.LineNoHolder), pGame.LineNoHolder, "Null", "Null", "Null");
            SaveData d = new SaveData(0, "SaveData");

            string jsonText = await FileIO.ReadTextAsync(f);

            JsonObject jsonObject = JsonObject.Parse(jsonText);
            JsonArray jsonArray = jsonObject["Groups"].GetArray();

            foreach (JsonValue groupValue in jsonArray)
            {
                JsonObject groupObject = groupValue.GetObject();
                SaveDataGroup group = new SaveDataGroup(groupObject["UniqueId"].GetString(),
                                                            groupObject["Title"].GetString(),
                                                            groupObject["Subtitle"].GetString(),
                                                            groupObject["ImagePath"].GetString(),
                                                            groupObject["Description"].GetString());

                foreach (JsonValue itemValue in groupObject["Items"].GetArray())
                {
                    JsonObject itemObject = itemValue.GetObject();
                    group.Items.Add(new SaveDataItem((int)itemObject["UniqueId"].GetNumber(),
                                                       itemObject["Title"].GetString(),
                                                       (int)itemObject["LineNo"].GetNumber(),
                                                       itemObject["ImagePath"].GetString(),
                                                       itemObject["Description"].GetString(),
                                                       itemObject["Content"].GetString()));
                }
                d.Groups.Add(group);
            }

            d.Groups[0].Items.RemoveAt(pSlot);
            d.Groups[0].Items.Insert(pSlot, newSave); //Watch out for 0, bad code

            using (Stream str = await f.OpenStreamForWriteAsync())
            {
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(SaveData));
                ser.WriteObject(str, d);
                await str.FlushAsync();
            }

            SharPy.Interface.clDialogs.SaveSuccess();

            return 1;
        }
    }
}
